#ifndef __EXTENSIONS__H__
#define __EXTENSIONS__H__

#include <QPluginLoader>
#include <QWidget>

#define ZTBoxInterface_IID "z.tbox"
/**
 * @brief  内建扩展类
 * @note   [可继承实现，可扩展实现(为 QWidget 套壳实现非侵入式扩展)]
 * @retval None
 */
class ZTBoxInterface : public QWidget
{
public:
    /**
     * @brief  [套壳]扩展类状态
     * @note   
     * @retval None
     */
    enum TaskState {
        None,

        Uninitialized,
        Initializing,
        Initialized,
    };

    /**
     * @brief  插件名称，建议以 ">" 开头
     * @note   [必须重写]
     * @retval 
     */
    virtual QString PluginName() = 0;
    /**
     * @brief  该插件是否应该永远显示
     * @note   [可重写]
     * @retval 
     */
    virtual bool PluginxAlwaysDisplay() {
        return false;
    };

    /**
     * @brief  插件在程序启动时是否是自动加载，也可作为是否会引发自动创建实例时导致卡顿的处理
     * @note   [可重写]
     * @retval 
     */
    virtual bool pluginAutoLoader() {
        return true;
    };
    /**
     * @brief  插件实例的QWidget提供方案，默认为继承者
     * @note   [当重写 pluginAutoLoader 时必须重写]
     * @retval 
     */
    virtual QWidget *pluginAutoLoaderProcess() {
        return this;
    };
    /**
     * @brief  插件在切换显示时是否需要重新定位到屏幕中心，当插件显示超过一半时建议重写
     * @note   [可重写]
     * @retval 
     */
    virtual bool pluginShowResetPosistion() {
        return false;
    };

public:
    TaskState taskState() const;
    void setTaskState(TaskState newTaskState);

    QWidget *pluginWidget();
    void setPluginWidget(QWidget *newPluginWidget);

private:
    TaskState m_taskState = Uninitialized;
    QWidget *m_pluginWidget = nullptr;
};

inline QWidget *ZTBoxInterface::pluginWidget()
{
    if (m_pluginWidget == nullptr) {
        setPluginWidget(pluginAutoLoaderProcess());
    }
    return m_pluginWidget;
}

inline void ZTBoxInterface::setPluginWidget(QWidget *newPluginWidget)
{
    if (m_pluginWidget == newPluginWidget)
        return;
    m_pluginWidget = newPluginWidget;
    setTaskState(Initialized);
}

inline ZTBoxInterface::TaskState ZTBoxInterface::taskState() const
{
    return m_taskState;
}

inline void ZTBoxInterface::setTaskState(ZTBoxInterface::TaskState newTaskState)
{
    if (m_taskState == newTaskState)
        return;
    m_taskState = newTaskState;
}

Q_DECLARE_INTERFACE(ZTBoxInterface, ZTBoxInterface_IID)

#endif  //!__EXTENSIONS__H__
